package com.shen.unsafe;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

public class MapTest {
    public static void main(String[] args) {
        /**
         * Map不安全
         */
      //  HashMap<Object, Object> map = new HashMap<>();

        /**
         * Collections 将不安全的HashMap封装成安全的Map
         */
        //Map<Object, Object> map = Collections.synchronizedMap(new HashMap<>());

        /**
         * JUC 方式 ConcurrentHashMap
         */
        ConcurrentHashMap map = new ConcurrentHashMap();
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
               map.put(Thread.currentThread().getName(),UUID.randomUUID().toString().substring(0, 5));
                System.out.println(map);
            },String.valueOf(i)).start();
        }

        /**
         * java.util.ConcurrentModificationException    并发修改错误
         */

    }
}
